<template>
  <div>
    <a-form-model>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="申请表" required>
          <SearchTable @f_setTable="s_setTable" field="apltable" :value="subCurrentObj.apltable" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="企业表">
          <SearchTable @f_setTable="s_setTable" field="enttable" :value="subCurrentObj.enttable" />
        </a-form-model-item> </a-col
      ><a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="是否必填" required>
          <RadioYesNo :value="subCurrentObj.require" field="require" @f_setYesNo="s_setYesNo" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="从表标题" required>
          <a-input style="width: 70%" v-model="subCurrentObj.label" placeholder="请输入节点标题" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="关联字段" required>
          <SearchField
            style="width: 70%"
            @f_setField="s_setField"
            :f_treeData="subCurrentObj.aplfieldlist"
            :value="subCurrentObj.relationfield"
            field="relationfield"
          />
        </a-form-model-item>
      </a-col>
    </a-form-model>
  </div>
</template>
<script>
import { subCurrentObj } from '@/utils/formassembly/generaltree.js'
import { findTable } from '@/api/base/table.js'
import { DATA_BASE_SFAPPROVE } from '@/store/mutation-types.js'
export default {
  data() {
    return {
      subCurrentObj: subCurrentObj,
      labelCol: { span: 4 }, // label宽
      wrapperCol: { span: 20 }, // 组件宽
    }
  },
  created() {},
  methods: {
    // 从表关联字段回调
    s_setField(value, field) {
      this.$set(this.subCurrentObj, field, value)
    },
    // 单选组件值
    s_setYesNo(value, field) {
      console.log('单选', value, field)
      this.$set(this.subCurrentObj, field, value)
    },
    // 回显选中的表
    async s_setTable(value, field, index) {
      this.subCurrentObj[field] = value
      // 如果点了清除按钮, 不做处理
      if (!value) {
        return
      }
      let fieldlist = []
      let res = await findTable({ database: DATA_BASE_SFAPPROVE, tableName: value })
      if (res.code == 0) {
        res.data.data.forEach((v) => {
            fieldlist.push({
              title: `${v.fieldName} - ${v.fieldDescription}`,
              value: v.fieldName,
            })
          })
      }
      // 根据选择的表类型加字段
      if (field == 'apltable') {
        this.subCurrentObj.aplfieldlist = fieldlist
        this.subCurrentObj.fieldlist = []
        fieldlist.forEach((v) => {
          this.subCurrentObj.fieldlist.push({
            t: v.value,
            b: undefined,
            hidden: false,
            disable: false,
            option: {
              type: undefined,
            },
            comments: v.title.substring(v.title.indexOf(' - ') + 3, v.title.length)
          })
        })
      } else if (field == 'enttable') {
        this.subCurrentObj.entfieldlist = fieldlist
        if (this.subCurrentObj.fieldlist && this.subCurrentObj.fieldlist.length > 0) {
          let entMap = {}
          // 企业表字段转MAP
          fieldlist.forEach((v) => {
            entMap[v.value] = v.value
          })
          // 根据之前的申请表字段进行匹配, 同名的字段赋值
          this.subCurrentObj.fieldlist.forEach((v) => {
            // console.log('---', v, entMap)
            if (v.t in entMap) {
              v.b = entMap[v.t]
            }
          })
        }
      }
      console.log('subCurrentObj', this.subCurrentObj, this.subCurrentObj.fieldlist)
    },
  },
  components: {
    SearchTable: () => import('@/views/businessAssembly/public/spareparts/searchTable'),
    RadioYesNo: () => import('@/views/businessAssembly/public/spareparts/radioYesNo'),
    SearchField: () => import('@/views/businessAssembly/public/spareparts/searchField')
  },
}
</script>

<style lang="scss" scoped>
</style>
